package com.mycjj.android.activity;

import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.InsuranceBaseBean;
import com.mycjj.android.bean.InsuranceBaseBean.InsuranceBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.util.Utils;

public class InsuranceActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_insurance);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setText("确定");
		mHeaderRightButton.setTextColor(getResources().getColor(R.color.white));
		mHeaderRightButton.setBackgroundResource(R.color.transparent);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("选择保险公司");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private ListView lv_insurance;
	private InsuranceAdapter adapter;
	private String token;
	static final int DATE_DIALOG_ID = 1;
	private int mYear;
	private int mMonth;
	private int mDay;
	private void init() {
		token = Preferences.getSharedPreferences(this, "user_token", "");
		initHeader();
		final Calendar c = Calendar.getInstance();
	    mYear = c.get(Calendar.YEAR);
	    mMonth = c.get(Calendar.MONTH);
	    mDay = c.get(Calendar.DAY_OF_MONTH);
		lv_insurance = (ListView) findViewById(R.id.lv_insurance);
		adapter = new InsuranceAdapter(this);
		lv_insurance.setAdapter(adapter);
		lv_insurance.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (beanList == null || beanList.size() == 0) {
					Log.e("mark", "callDetailList is empty!!");
					return;
				}
				currentInsuranceBean = beanList.get(position);
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			DatePickerDialog picker = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
			picker.setTitle("请选择投保日期");
			return picker;
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			String date = year+"-"+Utils.pad(monthOfYear+1)+"-"+Utils.pad(dayOfMonth);
			Preferences.setEditor(mApp, "applicant_date", date);
			dataManager.setCurrentInsuranceBean(currentInsuranceBean);
			InsuranceActivity.this.finish();
		}
	};

	private List<InsuranceBean> beanList;
	private InsuranceBean currentInsuranceBean;
	private void initData() {

		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		String storeid = Preferences.getSharedPreferences(mApp, "storeId", "");
		params.put("store_id", storeid);
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", token);
		request.requestInsuranceListWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						InsuranceBaseBean baseBean = ProjectDataManage.getInstance().getInsuranceBaseBean();
						if(baseBean != null){
							if(baseBean.getCode() == 200){
								beanList = baseBean.getData(); 
								adapter.setList(beanList);
							}else{
								Toast.makeText(InsuranceActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
							}
						}else{
							Log.e("mark", "baseBean is null!!!");
						}
					}
				});
	}

	public class InsuranceAdapter extends TAdapter<InsuranceBean> {

		public InsuranceAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_qiuzhu_view, null);
				holder = new ViewHolder();
				holder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_qz_title);
				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_qz_pnumber);
				holder.iv_call_phone = (ImageView) convertView.findViewById(R.id.iv_call_phone);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			InsuranceBean detail = mList.get(position);
			holder.tv_title.setText(detail.getTitle());
			holder.tv_number.setText(String.valueOf(detail.getTel()));
			holder.iv_call_phone.setVisibility(View.GONE);
			return convertView;
		}

		class ViewHolder {
			TextView tv_title;
			TextView tv_number;
			ImageView iv_call_phone;
		}

	}

}
